﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Threading;

namespace JasRibbonApplicatie.Classes
{
    class Emailer
    {
        //config
        private NetworkCredential credentials;
        public string Host = "127.0.0.1";
        public int Port = 25;

        public bool IsHtml = true;
        public SmtpDeliveryMethod SendUsing = SmtpDeliveryMethod.Network;//Network, PickupDirectory, SpecifiedPickupDirectory
        public bool UseSSL = true;
        public int AuthenticationMode = 1;//0 = No authentication, 1 = Plain Text, 2 = NTLM authentication

        //message
        public string From = string.Empty;

        public Emailer(string host, int port, string user="", string password="")
        {
            Host = host;
            Port = port;
            if(user=="" && password=="") AuthenticationMode = 0;
            else credentials = new NetworkCredential(user, password);
        }

        /// <summary>
        /// Send Email Message method.
        /// </summary>
        private void SendMessage(string to, string subject, string body, string[] attachmentPaths, string cc = "")
        {
            try
            {
                MailMessage oMessage = new MailMessage();
                SmtpClient smtpClient = new SmtpClient(Host);

                oMessage.From = new MailAddress(From);
                oMessage.To.Add(to);
                oMessage.Subject = subject;
                oMessage.IsBodyHtml = IsHtml;
                oMessage.Body = body;

                if (cc != "")
                    oMessage.CC.Add(cc);

                SendUsing = smtpClient.DeliveryMethod;

                if (AuthenticationMode > 0)
                {
                    smtpClient.Credentials = credentials;
                }

                smtpClient.Port = Port;
                smtpClient.EnableSsl = UseSSL;

                // Create and add the attachment
                if (attachmentPaths.Count()>0)
                {
                    foreach (var attachmentPath in attachmentPaths)
                        oMessage.Attachments.Add(new Attachment(attachmentPath));
                }

                try
                {
                    // Deliver the message    
                    smtpClient.Send(oMessage);

                }

                catch (Exception ex)
                {
                    ex.ToString();

                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

    }
}